/*
 * Copyright 2010 Kevin L'Huillier <klhuillier@gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package org.lhuillier.pwsafe.model;


/**
 * Determines how new passwords should be generated for a record.
 */
public class RecordPwPolicy implements PwPolicy {
    private boolean useLowercase;
    private boolean useUppercase;
    private boolean useDigits;
    private boolean useSymbols;
    private boolean useHexDigits;
    private boolean useEasyVision;
    private boolean makePronounceable;
    private int totalLength;
    private int minLowerLetters;
    private int minUpperLetters;
    private int minDigits;
    private int minSymbols;

    @Override
    public boolean getUseLowercase() {
        return useLowercase;
    }

    @Override
    public void setUseLowercase(boolean useLowercase) {
        this.useLowercase = useLowercase;
    }

    @Override
    public boolean getUseUppercase() {
        return useUppercase;
    }

    @Override
    public void setUseUppercase(boolean useUppercase) {
        this.useUppercase = useUppercase;
    }

    @Override
    public boolean getUseDigits() {
        return useDigits;
    }

    @Override
    public void setUseDigits(boolean useDigits) {
        this.useDigits = useDigits;
    }

    @Override
    public boolean getUseSymbols() {
        return useSymbols;
    }

    @Override
    public void setUseSymbols(boolean useSymbols) {
        this.useSymbols = useSymbols;
    }

    @Override
    public boolean getUseHexDigits() {
        return useHexDigits;
    }

    @Override
    public void setUseHexDigits(boolean useHexDigits) {
        this.useHexDigits = useHexDigits;
    }

    @Override
    public boolean getUseEasyVision() {
        return useEasyVision;
    }

    @Override
    public void setUseEasyVision(boolean useEasyVision) {
        this.useEasyVision = useEasyVision;
    }

    @Override
    public boolean getMakePronounceable() {
        return makePronounceable;
    }

    @Override
    public void setMakePronounceable(boolean makePronounceable) {
        this.makePronounceable = makePronounceable;
    }

    @Override
    public int getTotalLength() {
        return totalLength;
    }

    @Override
    public void setTotalLength(int totalLength) {
        this.totalLength = totalLength;
    }

    @Override
    public int getMinLowerLetters() {
        return minLowerLetters;
    }

    @Override
    public void setMinLowerLetters(int minLowerLetters) {
        this.minLowerLetters = minLowerLetters;
    }

    @Override
    public int getMinUpperLetters() {
        return minUpperLetters;
    }

    @Override
    public void setMinUpperLetters(int minUpperLetters) {
        this.minUpperLetters = minUpperLetters;
    }

    @Override
    public int getMinDigits() {
        return minDigits;
    }

    @Override
    public void setMinDigits(int minDigits) {
        this.minDigits = minDigits;
    }

    @Override
    public int getMinSymbols() {
        return minSymbols;
    }

    @Override
    public void setMinSymbols(int minSymbols) {
        this.minSymbols = minSymbols;
    }

    @Override
    public String toString() {
        return getClass().getName() + "[use="
                + (useLowercase ? " lowercase" : "")
                + (useUppercase ? " uppercase" : "")
                + (useDigits ? " digits" : "")
                + (useSymbols ? " symbols" : "")
                + (useHexDigits ? " hexdigits" : "")
                + (useEasyVision ? " easyvision" : "")
                + (makePronounceable ? " pronounceable" : "")
                + ";totalLength=" + totalLength + ";minLower=" + minLowerLetters
                + ";minUpper=" + minUpperLetters + ";minDigits=" + minDigits
                + ";minSymbols=" + minSymbols + "]";
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        RecordPwPolicy o = (RecordPwPolicy) obj;
        return useLowercase == o.useLowercase
                && useUppercase == o.useUppercase
                && useDigits == o.useDigits
                && useSymbols == o.useSymbols
                && useHexDigits == o.useHexDigits
                && useEasyVision == o.useEasyVision
                && makePronounceable == o.makePronounceable
                && totalLength == o.totalLength
                && minLowerLetters == o.minLowerLetters
                && minUpperLetters == o.minUpperLetters
                && minDigits == o.minDigits
                && minSymbols == o.minSymbols;
    }

    @Override
    public int hashCode() {
        int result = useLowercase ? 0x80000000 : 0;
        result ^= useUppercase ? 0x40000000 : 0;
        result ^= useDigits ? 0x20000000 : 0;
        result ^= useSymbols ? 0x10000000 : 0;
        result ^= useHexDigits ? 0x8000000 : 0;
        result ^= useEasyVision ? 0x4000000 : 0;
        result ^= makePronounceable ? 0x2000000 : 0;
        result ^= totalLength << 12;
        result ^= minLowerLetters << 9;
        result ^= minUpperLetters << 6;
        result ^= minDigits << 3;
        result ^= minSymbols;
        return result;
    }
}
